<template>
  <div class="push-log-detail w-full h-full display-flex-column" style="overflow: hidden">
    <a-table
      :loading="isTableLoading"
      class="dist-simple-table w-full"
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      :scroll="{ x: true, y: 700 }"
    >
      <template slot="operate" slot-scope="text,record">
        <div class="display-flex">
          <a-button type="primary" @click="rePullFromInv(record)">重拉</a-button>&nbsp;&nbsp;
<!--          <a-button type="default" @click="compareWithInv(record)">对比</a-button>-->
        </div>
      </template>
    </a-table>
  </div>
</template>
<script>
import sqlService from '@/api/sqlService'
import pushLogService from '@/api/pushLogService'

const columns = [
  {
    title: '项目代码',
    dataIndex: 'PROJECTCODE',
    width: '15%',
    align: 'center'
  },
  {
    title: '3.0统一办件号',
    dataIndex: 'UNIQUENAME',
    width: '15%',
    align: 'center'
  },
  {
    title: '请求时间',
    dataIndex: 'REQUESTTIME',
    width: '12%',
    align: 'center'
  },
  {
    title: '错误信息',
    dataIndex: 'ERRORMSG',
    align: 'left'
  },
  {
    title: '处理结果',
    dataIndex: 'HANDLERESULT'
  },
  {
    title: '操作',
    dataIndex: 'operate',
    width: '80px',
    scopedSlots: { customRender: 'operate' }
  }
]
export default {
  name: 'ErrorLogDetail',
  props: {},
  data () {
    return {
      selectedTab: this.$parent.tab,
      isTableLoading: false,
      columns: columns,
      pagination: {
        pageSize: 10, // 默认每页显示数量
        total: 0,
        current: 1,
        showSizeChanger: true, // 显示可改变每页数量
        showTotal: (total) => `共${total}条数据`, // 显示总数
        onChange: (current, pageSize) => {
          this.pagination.current = current
          this.pagination.pageSize = pageSize
          this.loadData(this.preParams)
        }
      },
      dataList: [],
      preParams: {}
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    async compareWithInv (record) {
      console.log('====record:', record)
    },
    async rePullFromInv (record) {
      if (record.isLoading) {
        return
      }
      const recordId = record.ID
      console.log('====> id信息：', recordId)
      // 获取数据
      const recordParams = { id: recordId }
      await pushLogService.rePullRecord(recordParams).then((d) => {
        const msgTip = (d === null || d.trim() === '' ? '已重新拉取数据' : d)
        this.$message.info(msgTip)
      })
      record.isLoading = true
      await this.loadData() // 刷新页面
    },
    async loadData (params = {}, toFirst) {
      this.selectedTab = this.$parent.tab
      this.preParams = params
      if (toFirst) {
        this.pagination.current = 1
      }
      this.isTableLoading = true
      if (this.selectedTab !== '错误日志') {
        console.log('其他扩展类型: ', this.selectedTab)
        this.isTableLoading = false
        return
      }
      const difSqlConfigName = 'listErrorLog'
      // const detailSqlConfigName = 'getPushLogDetail'
      console.log('====> ErrorLogDetail:', this.selectedTab)
      const sqlParam = {
        sqlConfigName: difSqlConfigName,
        pageIndex: this.pagination.current,
        pageSize: this.pagination.pageSize,
        params: Object.assign({}, params)
      }
      const data = await sqlService.pageQuery(sqlParam)
      const rows = data.rows
      // await this.doSpecial(rows, null, detailSqlConfigName)
      this.pagination.total = data.total
      this.dataList = rows
      this.isTableLoading = false
    },
    async doSpecial (rows, data, detailSqlConfigName) {
      if (rows.length <= 0) {
        return
      }
      const projectIdList = []
      const idMap = {}
      rows.forEach(item => {
        item.isLoading = false
        projectIdList.push(item.PID)
        idMap[item.PID] = item
      })
      if (!data) {
        data = await sqlService.listQuery({
          params: {},
          data: {
            sqlConfigName: detailSqlConfigName,
            params: {
              projectId: projectIdList
            }
          }
        })
      }
      const projectIdMap = {}
      data.forEach(item => {
        if (!projectIdMap[item.PID]) {
          projectIdMap[item.PID] = []
        }
        projectIdMap[item.PID].push(item)
      })
      projectIdList.forEach(projectId => {
        const stageList = []
        const dealIdList = []
        const list = projectIdMap[projectId]
        if (list && list.length > 0) {
          list.forEach(item => {
            if (dealIdList.indexOf(item.DEAL_ID) < 0) {
              dealIdList.push(item.DEAL_ID)
              const data = {
                dealState: item.F_DEAL_STATE,
                activityName: item.ACTIVITYNAME,
                responseData: item.F_RESPONSEDATA
              }
              stageList.push(data)
            }
          })
        }
        idMap[projectId].stageList = stageList
      })
    },
    init () {
      this.loadData()
    }
  }
}
</script>

<style scoped lang="less">
.push-log-detail {
  padding: 10px;

  /deep/ .dist-simple-table {
    display: flex;
    overflow: hidden;
    flex: 1;

    th {
      text-align: center;
    }

    .ant-spin-nested-loading {
      width: 100%;
      height: 100%;

      .ant-spin-container {
        width: 100%;
        height: 100%;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        align-items: flex-end;

        .ant-table {
          width: 100%;
          flex: 1;
          overflow: auto;
        }
      }
    }

  }

  /deep/ .dist-simple-table {
    .stage-item {
      .item-content {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }

}

</style>
